"use strict";

angular.module('app.sensor').controller('ParametersCtrl', function ($scope,$http) {
    $scope.insert = {};
    $scope.type_obj = {};
    // 协议类型
    $http.get(baseurl+'/select_ports').then(function(res){
       // console.log(res.data);
       if (res.data.ret == 1) {
          $scope.ports = res.data.info;
          if (res.data.info.length > 0) {
            $scope.insert.pro_type = res.data.info[0].port+'';
          }
       }
    });
    // 参数类型
    $http.get(baseurl+'/params_types').then(function(res){
       // console.log(res.data);
       if (res.data.ret == 1) {
          $scope.par_types = res.data.info;
          if (res.data.info.length > 0) {
            $scope.insert.type = res.data.info[0].id;
            for (var i = 0; i < res.data.info.length; i++) {
                var obj = res.data.info[i];
                $scope.type_obj[obj.id] = obj.name;
             }
          }
       }
    });
    
    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function () {
        }
    };
    $scope.select = function () {
        var page_obj = {
            page:$scope.paginationConf.currentPage,
            rows:$scope.paginationConf.itemsPerPage
        }
        $http.post(baseurl+'/list/select/device_parameters',page_obj).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                for (var i = 0; i < res.data.rows.length; i++) {
                    var obj = res.data.rows[i];
                    obj.typename = $scope.type_obj[obj.type];
                }
                $scope.datas = res.data.rows;
                $scope.paginationConf.totalItems = res.data.records;
            }           
        })
    }
    $scope.parshow = true;
    // 获取该端口下的所有表的字段
    function get_columns(){
        $http.get(baseurl+'/table_columns/log_'+$scope.insert.pro_type).then(function(res){
           // console.log(res.data);
            if (res.data.ret == 1) {
                $scope.par_column = res.data.info;
                if (res.data.info[0]) {
                    $scope.insert.key1 = res.data.info[0].COLUMN_NAME;
                }              
                
            }
        });
    }
    $scope.port_change = function(port){
        if ($scope.insert.type == '4') {
            get_columns();
            $scope.parshow = false;
        }else{
            $scope.parshow = true;
            $scope.insert.key1 = '';
        }
        
    }
    // 参数类型的改变
    $scope.par_change = function(type){
        // 字段参数
       if (type == '4') {
            get_columns();
            $scope.parshow = false;
        }else{
            $scope.parshow = true;
            $scope.insert.key1 = '';
        }
    }
    //检测是否为中文，true表示是中文，false表示非中文
    function isChinese(str){ 
        if(/.*[\u4e00-\u9fa5]+.*$/.test(str)) 
        { 
          return false; 
        } 
        return true; 
    } 
    $scope.insertdata = function () {
        // console.log($scope.insert.key1);
        var key = $scope.insert.key1;
        if (key == null || key == '' || !isNaN(Number(key)) || isChinese(key) == false) {
            new Notify('info','key不为空,不是数字类型,不能有中文'); 
            return;
        }
        if ($scope.insert.value1 == null || $scope.insert.value1 == '') {
            new Notify('info','value不为空'); 
            return;
        }
        $http.post(baseurl+'/data/device_parameters',$scope.insert).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                 $("#addAlert").modal("hide");
                 $scope.select();
            }else if (res.data.ret != -4){
                 new Notify('fail','key在设备表已存在');
            }
        });
    }

    $scope.updatedata = function () {
        delete $scope.editdata.id;
        delete $scope.editdata.typename;
        // console.log($scope.editdata);
        var key = $scope.editdata.key1;
        if (key == null || key == '' || !isNaN(Number(key)) || isChinese(key) == false) {
            new Notify('info','key不为空,不是数字类型,不能有中文'); 
            $scope.select();
            return;
        }
        if ($scope.editdata.value1 == null || $scope.editdata.value1 == '') {
            new Notify('info','value不为空'); 
            $scope.select();
            return;
        }
        $http.put(baseurl+'/data/device_parameters?id=='+$scope.editid,$scope.editdata).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                 $("#editAlert").modal("hide");
                 $scope.select();
            }else{
                 $scope.select();
                 new Notify('fail','key在设备表已存在');
            }
        });
    }
    $scope.editdata = {};
    $scope.showEditAlert = function (data) {
        $scope.editid = data.id;
        $scope.editdata = data;
        // $scope.editdata.oldkey1 = data.key1;
        $('#editAlert').modal('show');
    }
    $scope.showDelDialog = function (id,key) {
        // console.log(id,key);
        $scope.delid = id;
        $scope.delkey = key;
        $('#showDelDialog').modal('show');
    }

    $scope.deldevice = function () {
        $http.delete(baseurl+'/data/device_parameters?id=='+$scope.delid+'&key='+$scope.delkey).then(function(res){
                // console.log(res.data);
                if (res.data.ret == 1) {
                     $('#showDelDialog').modal('hide');
                     $scope.select();
                }
            });
    }

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
    

});
